/*******************************************************************************
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
String.prototype.trim= function() { 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 

String.prototype.ltrim = function(){ 
	return this.replace(/(^\s*)/g, ""); 
} 

String.prototype.rtrim = function(){ 
	return this.replace(/(\s*$)/g, ""); 
} 

String.prototype.endWith=function(str){
	if(str==null||str==""||this.length==0||str.length>this.length)
	  return false;
	if(this.substring(this.length-str.length)==str)
	  return true;
	else
	  return false;
	return true;
}

String.prototype.startWith=function(str){
	if(str==null||str==""||this.length==0||str.length>this.length)
	  return false;
	if(this.substr(0,str.length)==str)
	  return true;
	else
	  return false;
	return true;
}

/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/*This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "=" 
	var start = document.cookie.indexOf(prefix); 
	
	if (start==-1) {
		return null;
	}
	
	var end = document.cookie.indexOf(";", start+prefix.length);
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end) ;
	return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


/*select or unselect all checkbox which name are ckName*/
function checkAll(obj,ckName){
	$("input[name='"+ckName+"']").attr('checked',obj.checked);
}

function multiDelete(ckName,baseUrl){
	var cks=document.getElementsByName(ckName);
	var isSelected=false;

	for(var i=0;i<cks.length;i++){
		if(cks[i].checked){
			isSelected=true;
			break;
		}
	}
	if(isSelected){
		if(confirm("确定要删除选择的记录吗？")){
			var url=baseUrl!=null?baseUrl:location.href;
			var index=url.indexOf("?");
			if(index!=-1){
				url=url.substring(0,index);
			}
			url=url.replace("#","");
			url+="?cmd=multiDelete";
			for(var k=0;k<cks.length;k++){
				if(cks[k].checked){
					url+="&"+ckName+"="+cks[k].value;
				}
			}
			location.href=url;
		}
	}else{
		alert("请选择要删除的记录。");
	}
}


function deleteObj(idName,idVal,tips,baseUrl){
	if(!tips){
		tips="您确定要删除该记录吗？";
	}
	if(confirm(tips)){
		var url=baseUrl!=null?baseUrl:location.href;
		var index=url.indexOf("?");
		var queryParam=null;
		if(index!=-1){
			queryParam=url.substring(index+1);
			url=url.substring(0,index);
		}
		url=url.replace("#","");
		url+="?decorator=blank&cmd=delete&"+idName+"="+idVal;		
		if(queryParam!=null){
			url+="&"+queryParam;
		}
		location.href=url;
	}
}

function confirmAuction(msg){
	if(msg.length!=0)
	{
		var content;
		var tips="是否继续拍卖产品?";						
		for(var i=0;i<msg.length;i++){
			content=content+msg[i].batchId+" "+msg[i]+"</br>";
		}
		tips=content+tips;
		if(confirm(tips)){
			return true;
		}else{
			return false;
		}
	}else{
		alert('拍卖数量检测通过');
		return true;
	}
}

/*
function deletePic(idName,idVal,albumIdValue,tips,baseUrl){
	if(!tips){
		tips="您确定要删除该记录吗？";
	}
	if(confirm(tips)){
		var url=baseUrl!=null?baseUrl:location.href;
		var index=url.indexOf("?");
		var queryParam=null;
		if(index!=-1){
			queryParam=url.substring(index+1);
			url=url.substring(0,index);
		}
		url=url.replace("#","");
		//url+="?decorator=blank&cmd=delete&albumId="+albumIdValue+"&"+idName+"="+idVal;
		url+="?decorator=blank&cmd=delete&albumId="+albumIdValue+"&"+idName+"="+idVal;
		if(queryParam!=null){
			url+="&"+queryParam;
		}
		location.href=url;
	}
}
*/
/**
 *
 *	高亮显示列表中的行
 *
 * @param tableId
 */
function highlightTableRows(tableId) {
    var previousClass = null;
    var table = document.getElementById(tableId); 
    var tbody = table.getElementsByTagName("tbody")[0];
    var rows;
    if (tbody == null) {
        rows = table.getElementsByTagName("tr");
    } else {
        rows = tbody.getElementsByTagName("tr");
    }
    // add event handlers so rows light up and are clickable
    for (i=0; i < rows.length; i++) {
        rows[i].onmouseover = function() { previousClass=this.className;this.className+='_over' };
        rows[i].onmouseout = function() { this.className=previousClass };
    }
}


/**
 * 替换url中的page参数，用于数据分页显示
 */
function replacePageNum(url,pageNum,params){
	//把锚点标记先移除出来
	var anchor="";
	var index=url.indexOf("#");
	if(index!=-1){
		anchor=url.substring(index);
		url=url.substring(0,index);
	}
	//加上参数，原url附带查询参数，则需要保留
	if(params!=null&&params!=""){
		url+="?"+params;
	}
	//查询的页码需要替换
	var reg=/pageNum=[\d]*/;
	if(reg.test(url)){
		url=url.replace(reg,'pageNum='+pageNum);
	}else if(url.indexOf('?')!=-1){
		url+="&pageNum="+pageNum;
	}else{
		url+="?pageNum="+pageNum;
	}
	return url;
}

function replagePageSize(url,pageSize){
	var reg=/pageSize=[\d]*/;
	if(reg.test(url)){
		url=url.replace(reg,'pageSize='+pageSize);
	}else if(url.indexOf('?')!=-1){
		url+="&pageSize="+pageSize;
	}else{
		url+="?pageSize="+pageSize;
	}
	return url;
}

/**
 * Create Common Dialog
 */
function createDlg(dlgId,dlgMap){
	
	//加载所需js
	var dlgDiv=$(".ui-dialog")[0];
	if(dlgDiv==null){
		var jsArr=new Array();
		jsArr[0]=__ctxPath+"/resource/common/js/jquery/ui/ui.draggable.js";
		jsArr[1]=__ctxPath+"/resource/common/js/jquery/ui/ui.resizable.js";
		jsArr[2]=__ctxPath+"/resource/common/js/jquery/ui/ui.dialog.js";
		$importJs(jsArr,function(){});
	}
	//防止多次打开不能显示的问题
	$("#"+dlgId).dialog("destroy");
	if(document.getElementById(dlgId)==null){
		$(document.body).append("<div id='"+dlgId+"' style='display:none;'></div>");
	}
	$("#"+dlgId).css('display','block');
	var url=null;
	if(dlgMap){
		url=dlgMap.url;
	}
	if(url){
		$("#"+dlgId).html("<img src='"+__ctxPath+"/resource/common/images/data/ajax-loader.gif'/>正在加载....");
		if(url.indexOf("?")!=-1){
			url+="&";
		}else{
			url+="?";
		}
		url+="rand="+parseInt(100*Math.random());
		$("#"+dlgId).load(url,function(){
			if(dlgMap.onload)dlgMap.onload.call(this);
			//解决按钮浮在内容上的问题
			if(dlgMap.buttons){
				$("#detailDlg").css("margin-bottom","48px");
			}
		});
	}
	$("#"+dlgId).dialog(dlgMap);
}

function txtTimePicker(txtId){
	var jsArr=new Array();
	jsArr[0]=__ctxPath+"/resource/common/js/My97DatePicker/WdatePicker.js";
	$importJs(jsArr,function(){});
	
	$(function(){
		$('#'+txtId).click(function(){
			alert('funck');
			$('#'+txtId).append('<img src=""/>')
			$('#'+txtId).append('<img onclick="WdatePicker({el:$dp.$("'+txtId+'"),dateFmt:"yyyy-MM-dd HH:mm:ss",disabledDates:["%y-%M-%d #{%H-1}\:..\:..','%y-%M-%d #{%H+1}\:..\:.."]})" src="'+__ctxPath+'"/resource/common/js/My97DatePicker/skin/datePicker.gif" width="16" height="22" align="absmiddle"/>');
		});
	});
	
}

/**
 * create modal dialog
 */
function createModalDlg(dlgId,dlgMap){
	if(!dlgMap)dlgMap={};
	
	if(!dlgMap.modal){
		dlgMap.modal=true;
	}
	createDlg(dlgId,dlgMap);
}
/**
 * create overlay dialog
 */
function createOverlayDlg(dlgId,dlgMap){
	if(!dlgMap)dlgMap={};
	if(!dlgMap.overlay){
		dlgMap.modal=true;
		dlgMap.overlay={opacity: 0.5, background: 'black'};
	}
	createDlg(dlgId,dlgMap);
}

/* 打开详细对话框
 * @param idName 实体id字段名
 * @param idValue 实体id标识值
 * @param width 对话框的宽度
 * @param height 对话框的高度
 */
function openDetailDlg(url,title,width,height){
	var dlgMap={
		url:url,
		modal:true,
		overlay:{opacity: 0.5, background: 'black'},
		buttons:{
			"关闭":function(){
				$(this).dialog("close");
			}
		}
	};
	
	dlgMap.title=title?title:"详细信息";
	
	dlgMap.width=width?width:680;
	dlgMap.height=height?height:420;

	createDlg("detailDlg",dlgMap);
}

/**
 * 查看详细页
 * @param idName 实体id字段名
 * @param idValue 实体id标识值
 * @param width 对话框的宽度
 * @param height 对话框的高度
 */
function viewDetail(idName,idValue,title,width,height,urlInput){
	  var pageSizes=document.getElementsByName("pageSize");
				if(pageSizes!=null&&pageSizes.length>0){
					var pageSize=pageSizes[0]
					 pageSize.style.display = "none";
				}
	var url='';
  if (typeof urlInput != 'undefined') {
  	url = urlInput;
  } else {
		var url=location.href;
		var index=url.indexOf("?");
		if(index!=-1){
			url=url.substring(0,index);
		}
		url=url.replace("#","");
  }
	url+="?decorator=blank&cmd=viewDetail&"+idName+"="+idValue;
	openDetailDlg(url,title,width,height);
}

/**
 * 使输入框变成日期输入控件
 * @param txtInputId 输入控件的Id
 */
function applyDate(txtInputIds){
	var ids=txtInputIds.split(",");
	for(var cnt=0;cnt<ids.length;cnt++){
		var dateJs=document.getElementById("ui-datepicker-div");
		if(dateJs!=null){
			__loadDatePicker(ids[cnt]);
		}else{
			var urls=new Array();
			urls[0]=__ctxPath+"/resource/common/js/jquery/ui/ui.datepicker.js";
			urls[1]=__ctxPath+"/resource/common/js/jquery/ui/ui.datepicker-zh-CN.js";
			$importJs(urls,function(){
				__loadDatePicker(ids[cnt]);
			});
		}
	}
}


//提供日期控件

/*function applyDate(txtInputId){
	var urls=new Array();
	urls[0]=__ctxPath+"/resource/common/js/jquery/jquery-calendar.js";
	$importJs(urls,function(){});
	$('#'+txtInputId).focus(function(){
		$(this).attr('name','Date');
		$(this).calendar();
		$(this).attr('name',txtInputId);
	});
}*/
//提供日期时间空间
function applyDateTime(txtInputId){
	var urls=new Array();
	urls[0]=__ctxPath+"/resource/common/js/jquery/jquery-calendar.js";
	$importJs(urls,function(){});
	$('#'+txtInputId).focus(function(){
//		$(this).attr('name','DateAndTime');
		$(this).calendar();
//		$(this).attr('name',txtInputId);
	});
}

function applyDateTime1(txtInputId){
	var urls=new Array();
	urls[0]=__ctxPath+"/resource/common/js/jquery/jquery-calendar.js";
	$importJs(urls,function(){});
	$('#'+txtInputId).focus(function(){
		$(this).calendar();
	});
}
//提供时间控件
function applyTime(txtInputId){
	var urls=new Array();
	urls[0]=__ctxPath+"/resource/common/js/jquery/jquery-calendar.js";
	$importJs(urls,function(){});
	$('#'+txtInputId).focus(function(){
		$(this).attr('name','Time');
		$(this).calendar();
		$(this).attr('name',txtInputId);
	});
}

//比较开始时间和结束时间
		
function comptime(beginTime,endTime){
		var beginTimes=beginTime.substring(0,10).split('-');
		var endTimes=endTime.substring(0,10).split('-');
		
		beginTime=beginTimes[1]+'/'+beginTimes[2]+'/'+beginTimes[0]+' '+beginTime.substring(10,19);
		endTime=endTimes[1]+'/'+endTimes[2]+'/'+endTimes[0]+' '+endTime.substring(10,19);
	
		//alert(beginTime+endTime);
		//alert(Date.parse(endTime));alert(Date.parse(beginTime));
		 
		 //beginTime=beginTime.replace(regS,)
		
		var a =(Date.parse(endTime)-Date.parse(beginTime));
		
		if(a<0){
		//开始时间大于结束时间
		return -1;
		}else if (a>0){
		//开始时间小于结束时间
		return 1;
		}else if (a==0){
			//开始时间等于结束时间
		return 0;
		}else{
		return 'exception';}
	}



function __loadDatePicker(txtInputId){
	$("#"+txtInputId).datepicker(
			$.extend(
				{}, 
				$.datepicker.regional["zh-CN"], 
				{showStatus: true,
				minDate: new Date(),
				showOn: "both", buttonImage: __ctxPath+"/resource/common/images/function/calendar.gif", buttonImageOnly: true}
	));
}
/*
 * dynamic load the js
 * 
 * */
function $importJs(urls,callback){
	if (!(urls instanceof Array)) {
		return;
	}
	if(urls.length>=1){
		$.ajax({url:urls[0],
				async:false,//使用同步机制
				dataType:'script',
				success:function(){
					$importJs(urls.slice(1,urls.length),callback);
				}
		});		
	}else{
		if(callback!=null){
			callback.call(this);
		}
	}
}

/*****************************************
 * page start here
 * ***************************************
 */
function goPage(n){
	location.href=replacePageNum($("#_nav").attr('href'),n);
}

function first(){
	goPage(1);
}

function last(){
	var lastPage=$("#totalPages").val();
	goPage(lastPage);
}

function previous(){
	var curPage=parseInt($("#pageNum").val());
	curPage-=1;
	if(curPage<1)curPage=1;
	goPage(curPage);
}

function next(){
	var curPage=parseInt($("#pageNum").val());
	var totalPages=parseInt($("#totalPages").val());
	curPage+=1;
	if(curPage>totalPages)curPage=totalPages;
	goPage(curPage);
}

function changePageSize(sel){
	location.href=replagePageSize($("#_nav").attr('href'),sel.value);
}
/**
 * 跳转至第n页
 */
function jumpTo(){
	var pageNum=$("#navNum").val();
	var str=/^[1-9]\d*$/;
	if(str.test(pageNum)){
		goPage(pageNum);
	}else{
		alert("非法的页码!");
		$("#navNum").focus();
	}
}
/**
 * 导出选择时，更改导出链接url中的导出来全部项的参数.
 **/
function exportAllCheckChange(obj){
	var spanDiv=document.getElementById("divExportAll");
	if(spanDiv!=null){
		var childs=spanDiv.childNodes;
		for(var i=0;i<childs.length;i++){
			if(childs[i]!=null&&childs[i].tagName=="A"){
				var url=childs[i].href;
				if(obj.checked){
					if(url.indexOf("?")!=-1){
						url+="&";
					}else{
						url+="?";
					}
					childs[i].href=url+"exportAll=1";
				}else{
					childs[i].href=childs[i].href.replace("&exportAll=1","").replace("?exportAll=1").replace("exportAll=1");
				}
			}
		}
	}
}
/**
 * 加载相应的js，允许多文件上传
 */
function applyMultiUploadFile(){
	var urls=new Array();
	urls[0]=__ctxPath+"/resource/common/js/jquery/mutifile/jquery.MetaData.js";
	urls[1]=__ctxPath+"/resource/common/js/jquery/mutifile/jquery.MultiFile.js";
	urls[2]=__ctxPath+"/resource/common/js/jquery/mutifile/jquery.blockUI.js";
	$importJs(urls,function(){
		$(function(){ $.MultiFile({namePattern: '$name_$i'}) });
	});
}

function applyMultiUploadFlash(){
	var urls=new Array();
	urls[0]=__ctxPath+"/resource/common/js/jquery/mutifile/jquery.MetaData.js";
	urls[1]=__ctxPath+"/resource/common/js/jquery/mutifile/jquery.MultiFlash.js";
	urls[2]=__ctxPath+"/resource/common/js/jquery/mutifile/jquery.blockUI.js";
	$importJs(urls,function(){
		$(function(){ $.MultiFile({namePattern: '$name_$i'}) });
	});
}

/*****************************************
 * page end here
 * ***************************************
 */


